#ifndef __KEY_RECOMMANDER_H__
#define __KEY_RECOMMANDER_H__
#include "Dictionary.h"
#include <unordered_set>
#include "TcpConnection.h"
#include "RedisCache.h"
#include <memory>
using namespace std;

struct MyResult {
    MyResult(string word, int freq, int dist)
    : _word(word)
    , _freq(freq)
    , _dist(dist)
    {}
    string _word;
    int _freq;
    int _dist;
};

struct MyCompare {
    bool operator()(const MyResult& lhs, const MyResult& rhs) const {
        return lhs._dist < rhs._dist;
    }
};


class KeyRecommander {
using TcpConnectionPtr = shared_ptr<TcpConnection>;
public:
    /* KeyRecommander(string& query, const TcpConnectionPtr& conn) 
        : _queryWord(query)
          , _conn(conn)
    {}  */
    //KeyRecommander();
    void setQueryWord(string query);
    void setTcpConnectionPtr(const TcpConnectionPtr& conn);
    void execute();

private:
    void queryIndexTable();
    void statistic(set<int>& iset);
    size_t nBytesCode(const char ch);  
    std::size_t length(const std::string &str);
    int triple_min(const int &a, const int &b, const int &c);
    //计算最小编辑距离-包括处理中英文
    int editDistance(const std::string & lhs, const std::string &rhs);
    void response();
    
private:
    string _queryWord;
    TcpConnectionPtr _conn;
    priority_queue<MyResult, vector<MyResult>, MyCompare> _resultQue;
    unordered_set<string> _resultSet;
};

#endif
